package com.content.view.Share.Player
{
	import com.clerkq.FlvPlayer.FlvPlayer;
	import com.clerkq.FlvPlayer.VideoPlayer;
	import com.clerkq.SoundPlayer.SoundPlayer;
	import com.content.model.OnlineRes.OnlineResVO_Audio;
	import com.content.view.Component.Button.TextButtonColor;
	import com.library.core.IDisposable;
	import com.library.core.UIObject;
	import com.library.utils.Geometry;
	
	import flash.display.BlendMode;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.media.Sound;
	import flash.media.SoundMixer;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	import gs.TweenLite;
	
	public class AudioPlayer extends UIObject
	{
		public static const NAME:String							= 'AudioPlayer';
		public static const BUTTON_CLOSE:String						= NAME + "-" + 'ButtonClose';
		
		private var _sWidth:int;
		private var _sHeight:int;
		
		private var _stage:Stage;
		private var _container:Sprite;
		private var _bg:Shape;
		private var _vo:OnlineResVO_Audio;
		private var _btnClose:TextButtonColor;
		
		private var _player:SoundPlayer;
		
		public function AudioPlayer(){
			_container = new Sprite()
			
			_bg = new Shape();
			_container.addChild(_bg);
			
			_player = new SoundPlayer();
			_container.addChild(_player);
			
			_btnClose = new TextButtonColor("退出播放",	0x3b6893, 0xebebeb, 0xffffff, 0x000000, 8);
			_container.addChild(_btnClose);
			_btnClose.id = BUTTON_CLOSE;
		}
		public function Init(stage:Stage):void{
			_stage = stage;
		}
		
		public function Play(vo:OnlineResVO_Audio):void
		{		
			_stage.addChild(_container);
			
			_player.Play("assets/res/audio/demo.mp3");
			
			_stage.addEventListener(MouseEvent.CLICK, OnClick);
			
			_container.visible = false;
			_container.alpha = 0;
			TweenLite.to( _container, 0.5, { autoAlpha: 1 } );
			
			SetLayout(_sWidth, _sHeight);
		}
		
		private function Destory():void
		{
			if(_stage != null){
				_stage.removeEventListener(MouseEvent.CLICK, OnClick);
				_stage.removeChild(_container);
			}
		}
		
		public function SetLayout(sWidth:int, sHeight:int):void{
			_sWidth = sWidth;
			_sHeight = sHeight;
			
			if(_stage == null || !_stage.contains(_container)) return;
			
			_bg.graphics.clear();
			_bg.graphics.beginFill(0xffffff, 0.6);
			_bg.graphics.drawRect(0,0,sWidth, sHeight);
			
			_player.x = int((sWidth - _player.width)/2);
			_player.y = int((sHeight - _player.height)/2);
			
			_btnClose.x = int((sWidth - _btnClose.width)/2);
			_btnClose.y = int(_player.y + _player.height) + 5;
		}
		
		/**
		 * Event Handler 
		 */
		private function OnClick(evt:MouseEvent):void{
			if(evt.target is TextButtonColor){
				var txtBtn:TextButtonColor = evt.target as TextButtonColor;
				switch(txtBtn.id){
					case BUTTON_CLOSE:
						_player.Pause();
						Destory();
						break;
				}
			}
		}
		
		/*// Singleton
		private static var _instance:AudioPlayer;
		public static function get instance():AudioPlayer {
			if ( _instance == null ) _instance = new AudioPlayer( );
			return _instance as AudioPlayer;
		}*/
	}
}